﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            //预制数据
            UsersAndPoWOrAllAdd usersAndPoWOrAllAdd = new UsersAndPoWOrAllAdd();
            var Sqlsrt = "select *from Articles";
            var dt = usersAndPoWOrAllAdd.GetDataTable(Sqlsrt);
            //提取数据
            dataGridView1.DataSource = dt;
            
          
        }
        //按条件查找
        private void button1_Click(object sender, EventArgs e)
        {
            string text1 = textBox1.Text.Trim();
            UsersAndPoWOrAllAdd usersAndPoWOrAllAdd = new UsersAndPoWOrAllAdd();
            var Sqlsrt = string.Format("select * from Articles where Author like  '%{0}%'", text1);
             var dt = usersAndPoWOrAllAdd.GetDataTable(Sqlsrt);
            //提取数据
            dataGridView1.DataSource = dt;
        }
        //删除
        private void button3_Click(object sender, EventArgs e)
        {
            UsersAndPoWOrAllAdd usersAndPoWOrAllAdd = new UsersAndPoWOrAllAdd();
            var id = dataGridView1.SelectedRows[0];
            var rowId = (int)id.Cells["ID"].Value;
            var res = MessageBox.Show("确定要删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

            if (res.Equals(DialogResult.Yes)&&rowId>0)
            { 
            var Sqlsrt = string.Format("delete from Articles where ID={0}", rowId);
        var dt = usersAndPoWOrAllAdd.GetDataTable(Sqlsrt);
        //提取数据
        dataGridView1.DataSource = dt;
            }
            }
        //添加
        private void button4_Click(object sender, EventArgs e)
        {
            Form4 form4 = new Form4();
            form4.Show();
            this.Hide();
        }
        //修改
        private void button2_Click(object sender, EventArgs e)
        {
            UsersAndPoWOrAllAdd usersAndPoWOrAllAdd = new UsersAndPoWOrAllAdd();
            var id = dataGridView1.SelectedRows[0];
            var rowId = (int)id.Cells["ID"].Value;
            Form5 form5 = new Form5();
            form5.Show();
            this.Close();
            string nowtime= "SELECT GETDATE() AS CurrentDateTime";
            var Sqlsrt = string.Format("update Articles set Title='%{0}%', Author='%{1}%',CreatedTime={3} where ID={2}, )",form5.tt1,form5.tt2,rowId,nowtime);
            var dt = usersAndPoWOrAllAdd.GetDataTable(Sqlsrt);
            dataGridView1.DataSource = dt;
            //就是这里不行找不到问题在哪里最开始以为是代码问题
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}

